﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GrainManage.Data.Access;
using GrainManage.Data.Util;
using GrainManage.Data.Service;
using GrainManage.Entities.Column;
using GrainManage.Entities;
using GrainManage.Data.Pattern;

namespace GrainManage.Server.Factory
{
    public class ObjectFactory
    {
        private static readonly object lockObj = new object();
        private static readonly IPattern Pattern = null;
        private static readonly Dictionary<int, string> Permission = new Dictionary<int, string>();

        static ObjectFactory()
        {
            if (GetAppConfig("FilterModel").ToLower() == "like")
            {
                Pattern = new LikePattern();
            }
            else
            {
                Pattern = new RLikePattern();
            }

            var accessor = GenerateDbAccessor(null);
            AccountService = new AccountService(accessor);
            StatusService = new StatusService(accessor, Pattern);
            PatternService = new PatternService(accessor);
        }

        public static readonly AccountService AccountService = null;

        public static readonly StatusService StatusService = null;

        public static readonly PatternService PatternService = null;

        public static WeatherService GetWeatherService(BaseInputModel inputModel)
        {
            WeatherService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new WeatherService(accessor);
            }
            return service;
        }

        public static ColumnService GetColumnService(BaseInputModel inputModel)
        {
            ColumnService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new ColumnService(accessor);
            }
            return service;
        }

        public static ContactService GetContactService(BaseInputModel inputModel)
        {
            ContactService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new ContactService(accessor, Pattern);
            }
            return service;
        }

        public static DistrictService GetDistricService(BaseInputModel inputModel)
        {
            DistrictService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new DistrictService(accessor, Pattern);
            }
            return service;
        }

        public static ImageService GetImageService(BaseInputModel inputModel)
        {
            ImageService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new ImageService(accessor, Pattern);
            }
            return service;
        }

        public static PriceService GetPriceService(BaseInputModel inputModel)
        {
            PriceService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new PriceService(accessor, Pattern);
            }
            return service;
        }

        public static TradeService GetTradeService(BaseInputModel inputModel)
        {
            TradeService service = null;
            var accessor = GenerateDbAccessor(inputModel);
            if (accessor != null)
            {
                service = new TradeService(accessor, Pattern);
            }
            return service;
        }

        public static string GetAppConfig(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }

        private static string GetConnectionString(BaseInputModel inputModel)
        {
            string connectionString = null;
            if (inputModel != null && inputModel.PermissionID > 0)
            {
                if (!Permission.ContainsKey(inputModel.PermissionID))
                {
                    lock (lockObj)
                    {
                        if (!Permission.ContainsKey(inputModel.PermissionID))
                        {
                            var conn = AccountService.GetConnectionString(inputModel.PermissionID);
                            Permission[inputModel.PermissionID] = conn;
                        }
                    }
                }
                connectionString = Permission[inputModel.PermissionID];
            }
            else
            {
                connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
            }

            //if (string.IsNullOrEmpty(connectionString))
            //{
            //    connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
            //}
            return connectionString;
        }

        private static AccessBase GenerateDbAccessor(BaseInputModel inputModel)
        {
            AccessBase accessor = null;
            string connectionString = GetConnectionString(inputModel);
            if (!string.IsNullOrEmpty(connectionString))
            {
                var provider = new GrainManage.Data.Provider.MySqlProvider(connectionString);
                accessor = new GrainManage.Data.Access.ProcAccess(provider);
            }
            return accessor;
        }

    }
}